package com.android.model.net;

import android.util.Log;


import com.android.model.entity.res.RequestEntityUtils;
import com.android.util.DateUtils;

import org.joda.time.DateTime;

/**
 * Created by mj
 * on 2017/7/18.
 */

public class AppBaseNetWork {

    private static final String TAG = "AppBaseNetWork";

    public static final String BEFORE_MAIN_TRADE = "盘前交易";//盘前交易
    public static final String MAIN_TRADE = "交易中";//交易中
    public static final String AFTER_MAIN_TRADE = "盘后交易";//盘后交易
    public static final String CLOSE = "已收盘";//已收盘
    private static volatile AppBaseNetWork instance;
    private long requestTimeStamp = 0;//请求美东时间时的时间戳
    private RequestEntityUtils.StockDateEntity timeEntity;//时间entity
    public static final String americanStockOpenTime="09:30";//美东时间，美股开盘时间
    public static final String americanStockCloseTime="16:00";//美东时间美股收盘时间 特殊情况除外


    private AppBaseNetWork() {
    }

    public static AppBaseNetWork getInstance() {
        if (instance == null) {
            synchronized (AppBaseNetWork.class) {
                if (instance == null) {
                    instance = new AppBaseNetWork();
                }
            }
        }
        return instance;
    }

    /**
     * 请求接口时的时间戳
     *
     * @return 是否为今天
     */
    private boolean requestTimeStampIsToday(long timeStamp) {
        DateTime now = DateTime.now(DateUtils.ZONE_NY);
        DateTime requestDT = new DateTime(timeStamp).withZone(DateUtils.ZONE_NY);
        return now.getDayOfMonth() == requestDT.getDayOfMonth();
    }


    /**
     * 判断是否为开盘时间
     *
     * @param stateCallback 状态回调
     */
    public void isStockOpeningTime(StateCallback stateCallback) {
        if (requestTimeStamp != 0 && requestTimeStampIsToday(requestTimeStamp) && timeEntity != null) {
            if (stateCallback != null) {
                stateCallback.isSuccess(isDateRange());
            }
        } else {
            requestStockOpeningTime(stateCallback);
        }
    }

    /**
     * @return 是否在某个日期范围
     */
    private boolean isDateRange() {
        if (timeEntity == null) {
            return false;
        }
        long currentTimeStamp = getCurrentNYTimeStamp();
        long mainTimeStamp = timeEntity.MAIN;
        long closeTimeStamp = timeEntity.CLOSED;
//        DateTime cdTime = new DateTime(currentTimeStamp).withZone(ZONE_NY);
//        DateTime mainTime = new DateTime(mainTimeStamp).withZone(ZONE_NY);
//        DateTime closeTime = new DateTime(closeTimeStamp).withZone(ZONE_NY);
//        return cdTime.isAfter(mainTime) && cdTime.isBefore(closeTime);
        return currentTimeStamp >= mainTimeStamp && currentTimeStamp <= closeTimeStamp;
    }

    /**
     * 获取开盘时间戳
     *
     * @return 开盘时间戳
     */
    public long getMainStamp() {
        if (timeEntity != null) {
            return timeEntity.MAIN;
        }
        return 0;
    }

    /**
     * 获取 当天 收盘时间戳
     *
     * @return 收盘时间戳
     */
    public long getCloseStamp() {
        if (timeEntity != null) {
            return timeEntity.CLOSED;
        }
        return 0;
    }


    /**
     * 判断某个时间戳是否在盘中
     * @param time 时间戳
     * @return 时间戳是否在盘中
     */
    public boolean judgeTimeIsMain(long time) {
        if (timeEntity == null) {
            return false;
        }
        Log.e(TAG,"time="+time);
        Log.e(TAG,"main="+timeEntity.MAIN);
        Log.e(TAG,"close="+timeEntity.CLOSED);

        return time >= timeEntity.MAIN && time <= timeEntity.CLOSED;
    }

    /**
     * 获取交易时间
     * 盘前、盘中、盘后、收盘
     *
     * @return 字符串
     */
    public String tradeStateStr() {
        if (timeEntity == null) {
            return "";
        }
        long currentTimeStamp = getCurrentNYTimeStamp();
        long preMainTimeStamp = timeEntity.PRE_MAIN;
        long mainTimeStamp = timeEntity.MAIN;
        long closeTimeStamp = timeEntity.CLOSED;
        long afterTimeStamp = timeEntity.AFTER_MAIN;
//        DateTime cdTime = new DateTime(2017, 10, 18, 9,30,1, ZONE_NY);//测试美东时间交易时间
//        long currentTimeStamp=cdTime.toDate().getTime();

        if (currentTimeStamp >= preMainTimeStamp && currentTimeStamp < mainTimeStamp) {
            return BEFORE_MAIN_TRADE;
        } else if (currentTimeStamp >= mainTimeStamp && currentTimeStamp <= closeTimeStamp) {
            return MAIN_TRADE;
        } else if (currentTimeStamp > closeTimeStamp && currentTimeStamp <= afterTimeStamp) {
            return AFTER_MAIN_TRADE;
        } else {
            return CLOSE;
        }

//        DateTime cdTime = new DateTime(currentTimeStamp).withZone(ZONE_NY);
//        DateTime preTime = new DateTime(preMainTimeStamp).withZone(ZONE_NY);
//        DateTime mainTime = new DateTime(mainTimeStamp).withZone(ZONE_NY);
//        DateTime closeTime = new DateTime(closeTimeStamp).withZone(ZONE_NY);
//        DateTime afterTime = new DateTime(afterTimeStamp).withZone(ZONE_NY);
//        if (cdTime.isAfter(preTime) && cdTime.isBefore(mainTime)) {
//            return "盘前交易 ";
//        } else if (cdTime.isAfter(mainTime) && cdTime.isBefore(closeTime)) {
//            return "交易中 ";
//        } else if (cdTime.isAfter(closeTime) && cdTime.isBefore(afterTime)) {
//            return "盘后交易 ";
//        } else {
//            return "已收盘 ";
//        }
    }

    /**
     * 获取当前美东时间的时间戳
     *
     * @return 时间戳
     */
    private long getCurrentNYTimeStamp() {
        return DateTime.now(DateUtils.ZONE_NY).toDate().getTime();
    }


    /**
     * 获取服务器的时间戳
     *
     * @param stateCallback 状态回调
     */
    private void requestStockOpeningTime(final StateCallback stateCallback) {

//        String url = CommonURL.PUBLIC_SERVICE_TRADE_LINE + CommonURL.STOCK_OPENING_TIME;
//        requestTimeStamp = getCurrentNYTimeStamp();
//        OkGo.post(url)
//                .upString("\"" + requestTimeStamp + "\"")
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(String s, Call call, Response response) {
//                        Log.e(TAG, "s=" + s);
//                        StockTimeResponse stockTimeResponse = JsonUtil.parseJsonToBean(s, StockTimeResponse.class);
//                        if (stockTimeResponse != null && stockTimeResponse.getStatus().equals(CommonType.STATUS_TYPE_SUCCESS)) {
//                            timeEntity = stockTimeResponse.getData();
//                            if (stateCallback != null) {
//                                stateCallback.isSuccess(isDateRange());
//                            }
//                        } else {
//                            if (stateCallback != null) {
//                                stateCallback.isSuccess(false);
//                            }
//                        }
//                    }
//
//                    @Override
//                    public void onError(Call call, Response response, Exception e) {
//                        super.onError(call, response, e);
//                        if (stateCallback != null) {
//                            stateCallback.isSuccess(false);
//                        }
//                    }
//                });
    }

    /**
     * 状态回调
     */
    public interface StateCallback {
        void isSuccess(boolean isSuccess);
    }

}
